package com.visualexpress.datacreation;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.util.Iterator;
import java.util.List;

import com.visualexpress.dao.LocationDAO;
import com.visualexpress.domain.Location;

public class UtilGetInfoFromURL {
	public static void GetInfoFromURL(Location obj)
	{
		URL httpurl = null;
		HttpURLConnection httpConn = null;
		String line = "";
		
		String url=null;
		try {
			url = "http://maps.google.com/maps/geo?output=csv" +
				"&key=ABQIAAAA7XJiLye6Tmkn8kj2pwiLMxR2hIGQ36N7wA9SMLsZMNXCvGx-DRQEZ-0CVUH6D4VSZN-fH3XSHDChYg" +
				"&q=" + URLEncoder.encode(obj.getName(), "utf-8");
			
			httpurl = new URL(url);
			httpConn = (HttpURLConnection) httpurl.openConnection();
			httpConn.setRequestMethod("GET"); 
			httpConn.setDoOutput(true);
			httpConn.setDoInput(true);
			Thread.sleep((long)5000);
			BufferedReader in = new BufferedReader(new InputStreamReader(
					httpConn.getInputStream()));
			line = in.readLine();
			in.close();
			System.out.print(obj.getName());
			System.out.println("Line: " + line);
			
			String[] strs = line.split(",");
			obj.setLatitude(Double.parseDouble(strs[2]));
			obj.setLongitude(Double.parseDouble(strs[3]));
			
			
		}catch (UnsupportedEncodingException e1) {
			e1.printStackTrace();
		} catch (Exception e) {
			System.out.println("Error: " + e.getMessage());
		} 
		finally {
			if (httpConn != null)
				httpConn.disconnect();
			
		}
		
		
	}

}
